import { defineStore } from 'pinia'
import { ref } from 'vue'
import request from '../axios/request.js'
export const useCounterStore = defineStore('counter', () => {
    //声明数据
    const channelList = ref()


  //所有的火车
  const getList = function(){
    request({
      url: '/seat',
      method: 'get'
    }).then(res => {
      channelList.value = res.data.data
      console.log(channelList.value)
    }).catch(err => {
      console.log(err)
    })
  }
  getList();


    //判断登录是否成功的变量
    const isLogin = ref(false)

    //获取数据库中的所有的登录信息
    const loginList = ref()
    const userlogin = function(){
      request({
        url: '/user',
        method: 'get'
      }).then(res => {
        loginList.value = res.data.data
        // console.log(loginList.value)
      }).catch(err => {
        console.log(err)
      })
    }

    //获取成功登录者的信息
    const logininfoid = ref(0)

    //所有票分页的页数
    const pagination = ref(10)

    //购票信息的一些参数
    //车次
const trainNumber = ref('')
//车的起始站
const starting = ref('')
//车的终点站
const terminal = ref('')
//发车时间
const departure = ref('')
//到达时间
const arrival = ref('')
//座位类型
const seatType = ref('')
//price
const price = ref('')
//prive1
const price1 = ref('')
//grade1
const grade1 = ref('')
//grade2
const grade2 = ref('')
//seatno
const seatno = ref('')

  // post添加订单
  const orderList = function(){
    request({
      url: '/order',
      method: 'post',
      data: {
        seatID: trainNumber.value,
        starting: starting.value,
        terminal: terminal.value,
        departure: departure.value,
        arrival: arrival.value,
        seatType: seatType.value,
        price: priceall.value,
        grade: grade.value,
        userID: logininfoid.value,
        state:'已购买'
      }
    }).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }

  //判断是一等座的钱还是二等座的钱的变量
  const priceall = ref()

  //用于记录是几等座的变量
  const grade = ref()

  //放用户已购订单的变量
  const orderListsmall = ref()

  //传递路径参数进行request请求
  const selectorder = function(id){
    request({
      url: `selectorder/${id}`,
      method: 'get'
    }).then(res => {
      orderListsmall.value = res.data.data
    }).catch(err => {
      console.log(err)
    })
  }

  //要删除的id的变量
  const deleteuserid = ref()
  //要删除的车次变量
  const deletetrain = ref()

  //查找的起点站双向数据绑定
  const startingpiano = ref()
  //查找的终点站双向数据绑定
  const terminalpiano = ref()
  //查找发车时间的双向数据绑定
  const departurepiano = ref()
  //查找到达时间的双向数据绑定
  const arrivalpiano = ref()
  //座位类型的双向数据绑定
  const seatTypepiano = ref()

  //根据信息进行查找的ajax请求

  //存放查询后的结果
  const selecttrainList = ref()
  const selecttrain = function(){
    request({
      url: '/selecttrain',
      method: 'post',
      data: {
        starting: startingpiano.value,
        terminal: terminalpiano.value,
        departure: departurepiano.value,
        arrival: arrivalpiano.value,
        seatType: seatTypepiano.value
      }
    }).then(res => {
      selecttrainList.value = res.data.data
    }).catch(err => {
      console.log(err)
    })
  }

  //接收退票的用户id和车次信息
  const delectuserID = ref()
  const deleteseatID = ref()

  //存放订单编号
  const ordernumber = ref()

  // 退票的ajax请求
  const deletetrainorder = function(){
    request({
      url: '/deletetrainorder',
      method: 'delete',
      data: {
        ordernumber: ordernumber.value,
      }
    }).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }

  //存放新闻的变量
  const newsListdata = ref()

  //获取新闻资讯的ajax请求
  const newsList = function(){
    request({
      url: '/news',
      method: 'get'
    }).then(res => {
      newsListdata.value = res.data.data
      console.log(newsListdata.value)

    }).catch(err => {
      console.log(err)
    })
  }
newsList();

//改签的AJAX请求

// 改签传递的参数
const Gordernumber2 = ref()
const Gstarting2 = ref()
const Gterminal2 = ref()
const Gdeparture2 = ref()
const Garrival2 = ref()
const GseatType2 = ref()
const Gprice2 = ref()
const Ggrade2 = ref()


const fangfachangeorder = function(){
    request({
      url: '/changeorder',
      method: 'post',
      data: {
        ordernumber: Gordernumber2.value,
        starting: Gstarting2.value,
        terminal: Gterminal2.value,
        departure: Gdeparture2.value,
        arrival: Garrival2.value,
        seatType: GseatType2.value,
        price: Gprice2.value,
        grade: Ggrade2.value
      }
    }).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }

  //判断是否有权限访问admin模块的变量
const adminLogin = ref(false)

// //放用户的信息
// const user = ref()
// //获取用户的信息的ajax请求
// const selectuser = function(){
//   request({
//     url: '/logininfoid',
//     method: 'get'
//   }).then(res => {
//     console.log(res)
//     if(res.data.data == null){
//       adminLogin.value = false
//     }else{
//       adminLogin.value = true
//     }
//   }).catch(err => {
//     console.log(err)
//   })
// }

//控制按钮的变量
const value1 = ref(true);
//控制退票的变量
const value2 = ref(true);
//控制改签的变量
const value3 = ref(true);


//存放要删除的变量
const deletetrainid = ref()
//删除列车信息的ajax请求
const deletetraincaozhuo = function(){
  request({
    url: '/deletetrain',
    method: 'delete',
    data: {
      seatID:deletetrainid.value,
    }
  }).then(res => {
    console.log(res)
  }).catch(err => {
    console.log(err)
  })
}



  return { 
    channelList,
    getList,
    isLogin,
    userlogin,
    loginList,
    logininfoid,
    pagination,
    trainNumber,
    starting,
    terminal,
    departure,
    arrival,
    seatType,
    price,
    price1,
    grade1,
    grade2,
    seatno,
    orderList,
    grade,
    selectorder,
    orderListsmall,
    priceall,
    deleteuserid,
    deletetrain,
    ordernumber,
    selecttrain,
    selecttrainList,
    startingpiano,
    terminalpiano,
    departurepiano,
    arrivalpiano,
    seatTypepiano,
    delectuserID,
    deleteseatID,
    deletetrainorder,
    newsList,
    newsListdata,
    fangfachangeorder,
    Gordernumber2,
    Gstarting2,
    Gterminal2,
    Gdeparture2,
    Garrival2,
    GseatType2,
    Gprice2,
    Ggrade2,
    adminLogin,
    value1,
    value2,
    value3,
    deletetrainid,
    deletetraincaozhuo,
   }
})
